<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
// call this when atmost one property is changed..
function adjust_properties($semf_root, $doc, $component, $container, $property_group, $key, $value)
{
	// create the object for the corrosponding type
	$component_type = $component-> getAttribute('type');
	include_once($semf_root."page_designer/components/${component_type}/component.php");
	$class_name = ${component_type}."Component";
	
	$obj = new ${class_name}();

	// load properties under the node..

	$properties = load_properties($component);
	
	// build the dynamic properties..
	$properties = $obj-> build_properties($container, $properties, $property_group, $key, $value);
	
	// adjust the properties with the container..
	$properties = $obj-> adjust_properties_with_container($container, $properties);
	

	
	// modify child containers
	$containers = $obj->build_containers($container, $properties);
	adjust_containers($component, $containers, $doc);
	
	
	
	// create the xml tags for the properies..
	save_properties($component, $properties, $doc);
	
	// adjust the changes of child containers..
	foreach ( $component->childNodes as $child)
	{
		if( $child-> tagName != 'container')
		{
			continue;
		}
		// so the $child is a container..
		$child_component = $child->firstChild;
		if( $child_component != NULL )
		{
			adjust_properties($semf_root, $doc, $child_component, $child, NULL, NULL, NULL);
		}
	}
	
}

function adjust_containers($node, $containers, $doc)
{	
	$container_attributes = NULL;
	$value = NULL;
	
	$child_container = $node->firstChild;
	// modify or delete unwanted for existing ones
	while($child_container != NULL)
	{
		if( $child_container-> tagName != 'container')
		{
			$child_container =  $child_container-> nextSibling;
			continue;
		}
		$id = $child_container-> getAttribute('id');
		$container_record = $containers[$id];
		if( $container_record == NULL) // the container seems no longer exist..
		{
			$temp = $child_container;
			$child_container =  $child_container-> nextSibling;
			$node-> removeChild($temp);
			continue;
		}
		//if it is not NULL modify it
		$container_attributes = $containers[$id];
		$attri_keys = array_keys($container_attributes);
		foreach ($attri_keys as $attri )
		{
			$value = $container_attributes[$attri];
			$child_container-> setAttribute($attri, $value );
		}
		
		$child_container = $child_container-> nextSibling;
	}
	
	//create elements for new containers
	$key_node = NULL;
	$found = false;
	$container_keys = array_keys($containers);
	foreach($container_keys as $key)
	{
		$found = false;
		foreach( $node->childNodes as $existing_child)
		{
			$exisitings_id = $existing_child-> getAttribute('id');
			if( $exisitings_id == $key )
			{
				$found = true;
				break;
			}
		}
		
		if( !$found) //so just create new one..
		{
			$key_node = $doc->createElement('container');
			$key_node-> setAttribute('id', $key);
			
			$container_attributes = $containers[$key];
			$attri_keys = array_keys($container_attributes);
			foreach ($attri_keys as $attri )
			{
				$value = $container_attributes[$attri];
				$key_node-> setAttribute($attri, $value );
			}
			$node->appendChild($key_node);
		}
	}
}
?>